package com.soecode.lyf.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.soecode.lyf.constant.Constants;

public class UserInterceptor implements HandlerInterceptor {
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		SessionBean webUser = (SessionBean) request.getSession().getAttribute(Constants.SESSION_BEAN);
		String url = request.getRequestURI();
		if (url != null && !url.isEmpty()) {
			if (url.toLowerCase().indexOf("login") < 0 && url.toLowerCase().indexOf("validata") < 0) {
				if (null == webUser) {
					String localUrl = request.getScheme() + "://" + request.getServerName() + ":"
							+ request.getServerPort() + request.getContextPath() + "/";
					response.sendRedirect(localUrl);
					return false;
				} else {
					return true;
				}
			}
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {

	}

}
